import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * codeRule - 编码规则 - model
 * @date: 2018-6-29
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { queryCodeList, deleteCodeRule, addCodeValue, queryDist, deleteDist, queryDetail, addDistValue, addCodeDetail, deleteCodeDetail } from "../services/codeRuleOrgService";
export default {
  namespace: 'codeRuleOrg',
  state: {
    list: {
      data: {
        content: [],
        pagination: {}
      }
    },
    dist: {
      head: {},
      line: {
        list: [],
        pagination: {}
      }
    },
    detail: {
      data: {
        content: [],
        pagination: {}
      }
    },
    tenantStatus: {
      display: 'none',
      required: false
    },
    keyValue: {
      ruleId: null,
      ruleDistId: null
    },
    code: {
      LEVEL: [],
      UNITTYPE: [],
      FieldType: [],
      ResetFrequency: [],
      DateMask: [],
      Variable: []
    }
  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, lovCodes, res, FieldType, DateMask, ResetFrequency, UNITTYPE, Variable;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              lovCodes = payload.lovCodes;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              FieldType = res.FieldType, DateMask = res.DateMask, ResetFrequency = res.ResetFrequency, UNITTYPE = res.UNITTYPE, Variable = res.Variable;
              _context.next = 11;
              return put({
                type: 'queryInit',
                payload: {
                  FieldType: FieldType,
                  DateMask: DateMask,
                  ResetFrequency: ResetFrequency,
                  UNITTYPE: UNITTYPE,
                  Variable: Variable
                }
              });

            case 11:
              return _context.abrupt("return", res);

            case 12:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    fetchCode: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCode(_ref3, _ref4) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchCode$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(queryCodeList, payload);

            case 4:
              response = _context2.sent;
              data = getResponse(response);

              if (!data) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'queryCode',
                payload: data
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchCode);
    }),
    addCodeRule: /*#__PURE__*/_regeneratorRuntime.mark(function addCodeRule(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function addCodeRule$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(addCodeValue, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, addCodeRule);
    }),
    removeCode: /*#__PURE__*/_regeneratorRuntime.mark(function removeCode(_ref7, _ref8) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function removeCode$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(deleteCodeRule, payload);

            case 4:
              response = _context4.sent;
              return _context4.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, removeCode);
    }),
    fetchDist: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDist(_ref9, _ref10) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchDist$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call, put = _ref10.put;
              _context5.next = 4;
              return call(queryDist, payload);

            case 4:
              response = _context5.sent;
              data = getResponse(response);

              if (!data) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'queryDist',
                payload: data
              });

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, fetchDist);
    }),
    addDist: /*#__PURE__*/_regeneratorRuntime.mark(function addDist(_ref11, _ref12) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function addDist$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context6.next = 4;
              return call(addDistValue, payload);

            case 4:
              response = _context6.sent;
              return _context6.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, addDist);
    }),
    removeDist: /*#__PURE__*/_regeneratorRuntime.mark(function removeDist(_ref13, _ref14) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function removeDist$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call;
              _context7.next = 4;
              return call(deleteDist, payload);

            case 4:
              response = _context7.sent;
              return _context7.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, removeDist);
    }),
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref15, _ref16) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call, put = _ref16.put;
              _context8.next = 4;
              return call(queryDetail, payload);

            case 4:
              response = _context8.sent;
              data = getResponse(response);

              if (!data) {
                _context8.next = 9;
                break;
              }

              _context8.next = 9;
              return put({
                type: 'queryDetail',
                payload: data
              });

            case 9:
            case "end":
              return _context8.stop();
          }
        }
      }, fetchDetail);
    }),
    saveCodeDetail: /*#__PURE__*/_regeneratorRuntime.mark(function saveCodeDetail(_ref17, _ref18) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function saveCodeDetail$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              _context9.next = 4;
              return call(addCodeDetail, payload);

            case 4:
              response = _context9.sent;
              return _context9.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, saveCodeDetail);
    }),
    removeCodeDetail: /*#__PURE__*/_regeneratorRuntime.mark(function removeCodeDetail(_ref19, _ref20) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function removeCodeDetail$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref19.payload;
              call = _ref20.call;
              _context10.next = 4;
              return call(deleteCodeDetail, payload);

            case 4:
              response = _context10.sent;
              return _context10.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, removeCodeDetail);
    })
  },
  reducers: {
    settingOrgId: function settingOrgId(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        organizationId: action.payload.organizationId
      });
    },
    queryCode: function queryCode(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        list: _objectSpread({
          data: _objectSpread({
            content: action.payload.content,
            pagination: createPagination(action.payload)
          }, action.payload)
        }, state.data)
      });
    },
    queryDist: function queryDist(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        dist: {
          head: action.payload,
          line: _objectSpread(_objectSpread({}, action.payload.codeRuleDistDTOList), {}, {
            list: action.payload.codeRuleDistDTOList.content,
            pagination: createPagination(action.payload.codeRuleDistDTOList)
          })
        },
        keyValue: {
          ruleId: action.payload.ruleId
        }
      });
    },
    queryDetail: function queryDetail(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        detail: _objectSpread(_objectSpread({}, state.detail), {}, {
          data: {
            content: action.payload.codeRuleDetailDTOList,
            pagination: createPagination(action.payload)
          }
        }),
        keyValue: {
          ruleDistId: action.payload.ruleDistId
        }
      });
    },
    addNewDatail: function addNewDatail(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        detail: _objectSpread(_objectSpread({}, state.detail), {}, {
          data: _objectSpread(_objectSpread({}, state.detail.data), {}, {
            content: [].concat(_toConsumableArray(state.detail.data.content), [action.payload])
          })
        })
      });
    },
    changeFileType: function changeFileType(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        code: _objectSpread(_objectSpread({}, state.code), {}, {
          FieldType: state.code.FieldType.filter(function (type) {
            return type.value !== action.payload;
          })
        })
      });
    },
    resetFileType: function resetFileType(state, _ref21) {
      var payload = _ref21.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        code: _objectSpread(_objectSpread({}, state.code), {}, {
          FieldType: payload
        })
      });
    },
    queryInit: function queryInit(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        code: _objectSpread(_objectSpread({}, state.code), action.payload)
      });
    },
    updateState: function updateState(state, _ref22) {
      var payload = _ref22.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};